﻿using System;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WWF
{
    public class Application
    {
        #region API
        
        [DllImport("User32.dll")]
        private static extern IntPtr GetDC(IntPtr hwnd);
        
        [DllImport("User32.dll")]
        private static extern void ReleaseDC(IntPtr hwnd, IntPtr dc);
        
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetCursorPos(out POINT lpPoint);
        
        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int X;
            public int Y;
            
            public POINT(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }
        
        #endregion
        
        private static BufferedGraphicsContext currentContext;
        private static BufferedGraphics buffered;
        
        public static void Run(WebForm form)
        {
            form.Show();
            
            while (form.Visible)
            {
                try
                {
                    IntPtr desktopPtr = GetDC(IntPtr.Zero);
                    Graphics g1 = Graphics.FromHdc(desktopPtr);
                    currentContext = BufferedGraphicsManager.Current;
                    
                    buffered = currentContext.Allocate(g1, new Rectangle(form.Location.X, form.Location.Y, form.Size.Width, form.Size.Height));
                    
                    buffered.Graphics.FillRectangle(Brushes.Teal,
                        new Rectangle(new Point(form.Location.X, form.Location.Y - 50), new Size(form.Size.Width, 50)));
                    
                    if (form.Icon != null)
                        buffered.Graphics.DrawIcon(form.Icon, new Rectangle(new Point(form.Location.X, form.Location.Y - 25), new Size(16, 16)));
                    
                    buffered.Graphics.FillRectangle(new SolidBrush(form.BackgroundColor), new Rectangle(form.Location, form.Size));
                    buffered.Graphics.DrawString(form.Title, new Font("Consolas", 12), Brushes.Black, form.Location.X + 10, form.Location.Y - 20);
                    
                    POINT p;
                    if (GetCursorPos(out p))
                    {
                        form.OnMouseMove(new System.Windows.Forms.MouseEventArgs(System.Windows.Forms.MouseButtons.None, 0, p.X, p.Y, 0));
                        
                        form.Cursor.Draw(buffered.Graphics, new Rectangle(p.X, p.Y, form.Cursor.Size.Width, form.Cursor.Size.Height));
                    }
                    
                    form.OnPaint(new PaintEventArgs(buffered.Graphics, new Rectangle(form.Location, form.Size)));
                    
                    buffered.Render();
                    
                    buffered.Graphics.Clear(form.BackgroundColor);
                    
                    buffered.Graphics.Dispose();
                    ReleaseDC(IntPtr.Zero, desktopPtr);
                }
                catch
                {
                }
            }
        }
    }
}